package com.lengyu.iottransfer.gateway;

import com.lengyu.iottransfer.gateway.tcp.TcpServer;
import com.lengyu.iottransfer.gateway.udp.UdpServer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@MapperScan("com.lengyu.iottransfer.gateway.mapper")
@EnableScheduling // 开启定时任务功能
@EnableTransactionManagement
@EnableCaching
@SpringBootApplication
@EnableAsync
public class IotTransGatewayApplication implements CommandLineRunner {

    private final UdpServer udpServer;
    private final TcpServer tcpServer;

    public IotTransGatewayApplication(UdpServer udpServer, TcpServer tcpServer) {
        this.udpServer = udpServer;
        this.tcpServer = tcpServer;
    }


    public static void main(String[] args) {
        SpringApplication.run(IotTransGatewayApplication.class, args);
    }

    @Async
    @Override
    public void run(String... args) throws Exception {
        /**
         * 使用异步注解方式启动netty udp服务端服务
         */
        udpServer.bind(9999);

        /**
         * 使用异步注解方式启动netty udp服务端服务
         */
        udpServer.bindForZhongYiShuiBiao(9997);

        /**
         * 使用异步注解方式启动netty tcp服务端服务
         */
        tcpServer.bind(9996);

        /**
         * 使用异步注解方式启动netty tcp服务端服务
         */
        tcpServer.bindForShuiBiao(9995);
    }
}
